﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System;
using NUnitGrid.Framework;
using NUnitGrid.Protocol;
using NUnitGrid.Protocol.Commands;


namespace NUnitGrid.Server.Plugins.Nodes.Node.States
{
    public class RunningTaskState<T> : IGridNodeState, IGridTaskTransactionListener, IRunOnGridTaskListener
        where T : IGridNode
    {
        private readonly IGridNodeMetrics gridNodeMetrics;
        private readonly T node;
        private readonly INodeAvailableListener<T> nodeAvailableListener;
        private readonly IGridNodeListener<IGridNodeMetrics> nodeListener;
        private readonly IGridProtocol protocol;
        private readonly ISchedule schedule;
        private readonly IStateEngine<IGridNodeState> stateEngine;
        private readonly IServerStateFactory stateFactory;
        private readonly IRunOnGridTask task;
        private readonly IRunOnGridTaskListener taskListener;

        public RunningTaskState(IStateEngine<IGridNodeState> stateEngine, IServerStateFactory stateFactory,
                                ISchedule schedule,
                                IGridProtocol protocol, IRunOnGridTask task,
                                IRunOnGridTaskListener taskListener,
                                IGridNodeListener<IGridNodeMetrics> nodeListener,
                                IGridNodeMetrics gridNodeMetrics, T node,
                                INodeAvailableListener<T> nodeAvailableListener)
        {
            this.stateEngine = stateEngine;
            this.stateFactory = stateFactory;
            this.schedule = schedule;
            this.protocol = protocol;
            this.task = task;
            this.taskListener = taskListener;
            this.nodeListener = nodeListener;
            this.gridNodeMetrics = gridNodeMetrics;
            this.nodeAvailableListener = nodeAvailableListener;
            this.node = node;
        }

        public void OnEntering()
        {
            gridNodeMetrics.OnTaskStarted();
            var runningTask = new NodeRunOnGridTask(this, protocol, task, this);
            runningTask.SendCommand();
        }

        public void Start()
        {
        }

        public void OnConnectedChanged(bool connected)
        {
            if (!connected)
            {
                gridNodeMetrics.FailedTasksCount++;
                nodeListener.OnChange(gridNodeMetrics);
                nodeAvailableListener.OnNodeAvailable(node, NodeAvailability.Offline);
                stateEngine.ChangeState(stateFactory.CreateLinkNotConnectedState());
                taskListener.OnTaskCompleted(task, false, "Abandoned. Link lost to node", gridNodeMetrics.MachineName);
            }
        }

        public void RunTask(IRunOnGridTask task, IRunOnGridTaskListener taskListener,
                            IGridNodeListener<IGridNodeMetrics> nodeListener)
        {
            throw new NotImplementedException();
        }

        void IGridTaskTransactionListener.OnTaskStarted(object sender)
        {
            nodeListener.OnChange(gridNodeMetrics);
        }

        void IGridTaskTransactionListener.OnTaskCompleted(object sender, bool succeeded, string textOutput,
                                                          string machineName)
        {
            if (!succeeded)
            {
                gridNodeMetrics.FailedTasksCount++;
            }
            gridNodeMetrics.OnTaskCompleted();
            stateEngine.ChangeState(stateFactory.CreateAvailableState());
            nodeListener.OnChange(gridNodeMetrics);
        }

        public void OnTaskStarted(IRunOnGridTask sender)
        {
            taskListener.OnTaskStarted(sender);
        }

        public void OnTaskCompleted(IRunOnGridTask sender, bool succeeded, string textOutput, string machineName)
        {
            taskListener.OnTaskCompleted(sender, succeeded, textOutput, machineName);
        }
    }
}